package es

import (
	"github.com/gogf/gf/text/gstr"
)

// -------------------------------------------- Where  -------------------------------------------
// where
func (m *Model) Where(name, operator string, value interface{}) *Model {
	if gstr.InArray(operators, operator) == false {
		value = string(operator)
		operator = "="
	}
	if operator == "=" {
		m.Term(name, value)
	}
	if operator == ">" {
		m.Range(name, Gt(value))
	}
	if operator == ">=" {
		m.Range(name, Gte(value))
	}
	if operator == "<" {
		m.Range(name, Lt(value))
	}
	if operator == "<=" {
		m.Range(name, Lte(value))
	}
	if operator == "like" {
		m.MustMatch(name, value)
	}
	if operator == "query_string" {
		m.MustMatchPhrase(name, value)
	}
	if operator == "regexp" {
		m.MustRegexp(name, value.(string))
	}
	if operator == "wildcard" {
		m.MustWildcard(name, value.(string))
	}
	if operator == "script" {
		m.MustScript(name, value.(string))
	}
	if operator == "exists" {
		m.MustExist(name)
	}
	return m
}

// where not
func (m *Model) WhereNot(name, operator string, value interface{}) *Model {
	if gstr.InArray(operators, operator) == false {
		value = string(operator)
		operator = "="
	}
	if operator == "=" {
		m.MustNotTerm(name, value)
	}
	if operator == ">" {
		m.MustNotRange(name, Gt(value))
	}
	if operator == ">=" {
		m.MustNotRange(name, Gte(value))
	}
	if operator == "<" {
		m.MustNotRange(name, Lt(value))
	}
	if operator == "<=" {
		m.MustNotRange(name, Lte(value))
	}
	if operator == "like" {
		m.MustNotMatch(name, value)
	}
	if operator == "exists" {
		m.MustNotExist(name)
	}
	return m
}

// where or
func (m *Model) WhereOr(name, operator string, value interface{}) *Model {
	if gstr.InArray(operators, operator) == false {
		value = string(operator)
		operator = "="
	}
	if operator == "=" {
		m.ShouldTerm(name, value)
	}
	if operator == ">" {
		m.ShouldRange(name, Gt(value))
	}
	if operator == ">=" {
		m.ShouldRange(name, Gte(value))
	}
	if operator == "<" {
		m.ShouldRange(name, Lt(value))
	}
	if operator == "<=" {
		m.ShouldRange(name, Lte(value))
	}
	if operator == "like" {
		m.ShouldMatchPhrase(name, value)
	}
	if operator == "exists" {
		m.ShouldExist(name)
	}
	return m
}

// where or in
func (m *Model) WhereOrIn(name string, value []string) *Model {
	return m.ShouldTerms(name, value)
}

// where or between
func (m *Model) WhereOrBetween(name, first_value, last_value string) *Model {
	return m.ShouldRange(name, Gte(first_value), Lte(last_value))
}

// where like
func (m *Model) WhereOrLike(name string, value interface{}) *Model {
	return m.ShouldMatch(name, value)
}

// where or null
func (m *Model) WhereOrNull(name string, exists ...bool) *Model {
	return m.ShouldExist(name)
}

// where between
func (m *Model) WhereBetween(name, first_value, last_value string) *Model {
	return m.Range(name, Gte(first_value), Lte(last_value))
}

// where not between
func (m *Model) WhereNotBetween(name, first_value, last_value string) *Model {
	return m.MustNotRange(name, Gte(first_value), Lte(last_value))
}

// where in
func (m *Model) WhereIn(name string, value []string) *Model {
	return m.Terms(name, value)
}

// where like
func (m *Model) WhereLike(name string, value interface{}) *Model {
	return m.MustMatch(name, value)
}

// where not in
func (m *Model) WhereNotIn(name string, value []string) *Model {
	return m.MustNotTerm(name, value)
}

// where exists
func (m *Model) WhereExists(name string, exists ...bool) *Model {
	if len(exists) > 0 {
		m.MustNotExist(name)
	} else {
		m.MustExist(name)
	}
	return m
}

// where null
func (m *Model) WhereNull(name string, exists ...bool) *Model {
	return m.MustNotExist(name)
}

// where not null
func (m *Model) WhereNotNull(name string, exists ...bool) *Model {
	return m.MustExist(name)
}

// where not like
func (m *Model) WhereNotLike(name string, value interface{}) *Model {
	return m.MustNotMatch(name, value)
}